<template> 
    <div class="login_container">    
        <img :src="imgSrc"  class="background" width="100%" height="100%" alt="" />   
        <!-- 登陆块 -->
        <div class="login_box">
            <!-- 商标 -->
            <div class="avatar_box">
            <img  class="logo" src="../assets/images/food.png" alt/>
            </div>
            <!-- 表单区域 -->
            <!-- model用来传输数据，rules定义规则 -->
            <el-form ref="loginFormRef" :rules="loginRules" :model="loginForm" class="login_form" label-position="0px">
                <!-- 用户名项 -->
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username"  prefix-icon="iconfont czx-user"></el-input>
                </el-form-item>
                <!-- 密码项 -->
                <el-form-item prop="password">
                    <el-input v-model="loginForm.password" prefix-icon="iconfont czx-mima" type="password"></el-input>
                </el-form-item>
                <!-- 按钮项 -->
                <el-form-item class="btns">
                    <el-button type="primary" @click="login()">登录</el-button>
                    <el-button type="info" @click="resetLoginForm()">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script>
export default {
    data(){
        return{
            //表单数据
            loginForm:{
                username:"admin",
                password:"123456"
            },
            //验证对象
            loginRules:{
                //校验用户名
                username:[
                    //必填项验证
                    { required: true, message: '请输入用户名', trigger: 'blur' }, 
                    //长度验证
                    { min: 3, max: 12, message: '长度在 3 ~ 12 个字符', trigger: 'blur' }     
                ],
                //校验密码
                password:[
                    //必填项验证
                    { required: true, message: '请输入密码', trigger: 'blur' }, 
                    //长度验证
                    { min: 6, max: 14, message: '长度在 6 ~ 14 个字符', trigger: 'blur' } 
                ],

            },
            imgSrc:require('../assets/images/beijing.jpg'),
        };
    },
    methods:{
        //重置表单内容
        resetLoginForm(){
            this.$refs.loginFormRef.resetFields();
        },
        //处理登录的方法
        login(){
            //1.验证校验规则
            this.$refs.loginFormRef.validate(async valid =>{
                if(!valid)return;//验证失败直接结束
                //以下为获取数据代码格式
                const {data:res} = await this.$http.post("login",this.loginForm);//访问后台:登录（提交表单）用post方法
                if(res.flag == "ok"){
                    window.sessionStorage.setItem("user",res.user);//存储user对象   
                    this.$message.success("登录成功");//信息提示
                    this.$router.push({path:"/home"});//页面路由跳转                                
                }else{
                    this.$message.error("登录失败，用户名或密码错误");//错误提示
                }
            })       
        }
  
    },
    

}
</script>
<style scoped>
.background{
    height: 100%;
    width:100%; /**宽高100%是为了图片铺满屏幕 */
    z-index:-1;
    position: absolute;
}
.login_container{
    height: 100%;
}
.login_box{
    width: 450px;
    height: 300px;
    background-color: whitesmoke;
    border-radius: 5px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    
}
.avatar_box{
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 5px;
    box-shadow: 0 0 2px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%,-50%);
    background-color: white;
}
.logo{
        width:100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;        
}
.btns{
    display: flex;
    justify-content: flex-end;
}
.login_form{
    position: absolute;
    bottom: 0%;
    width: 100%;
    padding: 0 10px;
    box-sizing: border-box;
}
</style>